package com.wf.day_0619.test1;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @Author fengqq
 * @Description TODO
 * @Date 2024/6/19  11:28
 */
public class TestAnnotation {

    //测试 获取BookStore类上面的注解 以及 属性信息
    @Test
    public void testAnnotationClass() {
        //1.获取BookStore类的类类型
        Class<BookStore> aClass = BookStore.class;
        //2.判断 BookStore类上面是否有Book注解
        if (aClass.isAnnotationPresent(Book.class)) {
            Book book = aClass.getAnnotation(Book.class);
            getInfo(book);
        } else {
            System.out.println("当前类上面没有book注解");
        }

    }

    //测试 获取BookStore类中的属性上面的注解 以及 属性信息
    @Test
    public void testAnnotationField() {
        Class<BookStore> aClass = BookStore.class;

        Field[] fields = aClass.getDeclaredFields();

        for (Field field : fields) {

            if (field.isAnnotationPresent(Book.class)) {

                Book book = field.getAnnotation(Book.class);
                getInfo(book);
            }

        }

    }

    //测试 获取BookStore类中的方法上面的注解 以及 属性信息
    @Test
    public void testAnnotationMethod() {

        Class<BookStore> aClass = BookStore.class;

        Method[] methods = aClass.getDeclaredMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(Book.class)) {
                Book book = method.getDeclaredAnnotation(Book.class);
                getInfo(book);
            }
       }
    }

    private static void getInfo(Book book) {
        System.out.println("书名："+ book.value());
        System.out.println("作者："+ Arrays.toString(book.authors()));
        System.out.println("价格："+ book.price());
        System.out.println("-------------------------");
    }
}
